package cn.JHai.rbac.service.imp;

import cn.JHai.rbac.domain.Department;
import cn.JHai.rbac.mapper.DepartmentMapper;
import cn.JHai.rbac.query.PageResult;
import cn.JHai.rbac.query.QueryObject;
import cn.JHai.rbac.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentImp implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public int Delete(Long id) {
        return departmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Department get(Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Department> findAll() {
        return departmentMapper.selectAll();
    }

    @Override
    public int saveOrUpdata(Department department) {
        //是插入
        if(department.getId()==null){
            return departmentMapper.insert(department);
        }else {
            //跟新
            return departmentMapper.updateByPrimaryKey(department);
        }
    }

    @Override
    public PageResult query(QueryObject queryObject) {

        Long count = departmentMapper.selectForCount(queryObject);
        //List<Department> departments =null;
        /*if(count<=3l){
            //departments = departmentMapper.selectAll();
        }
        else{
            //departments = departmentMapper.pageSelect(queryObject);
        }*/
        return new PageResult(queryObject.getCurrentPage(),queryObject.getPageSize(),departmentMapper.pageSelect(queryObject),count);
    }
}
